﻿using CuaHangLego.Bussiness;
using CuaHangLego.DataAccess;
using CuaHangLego.Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CuaHangLego.UI
{
    public partial class SanPhamDetail : Form
    {
        private SanPhamKho sp;
        private SanPhamKhoBSS bss;
        string imgName = "";
        string imgURL = "";
        public SanPhamDetail(SanPhamKhoBSS bss, SanPhamKho sp)
        {
            InitializeComponent();
            this.sp = sp;
            this.bss = bss;
        }

        private void SanPhamDetail_Load(object sender, EventArgs e)
        {
            cbxChuDeSP.DataSource = ChuDeDA.Instance.GetAll();
            if (this.sp != null)
            {
                txtMaSoSP.Text = sp.MaSo.ToString();
                txtTenSP.Text = sp.Ten;
                txtSoManhSP.Text = sp.SoManhGhep.ToString();
                txtGiaBanSP.Text = sp.GiaBan.ToString();
                txtGiaMuaSP.Text = sp.GiaMua.ToString();
                txtBaoHanhSP.Value = sp.BaoHanh;
                txtNgaySXSP.Value = sp.NgaySX;
                cbxChuDeSP.SelectedIndex = cbxChuDeSP.FindStringExact(sp.ChuDe.Ten);
                chkbxDangKinhDoanh.Checked = sp.IsVisible;
                pictureSP.ImageLocation = "../../img/"+ sp.HinhAnh;
                this.buttonLuu.Enabled = true;
                this.buttonThemSP.Enabled = false;
            }
            else
            {
                sp = new SanPhamKho();
                this.buttonLuu.Enabled = false;
                this.buttonThemSP.Enabled = true;
            }
        }

        private void buttonThemSP_Click(object sender, EventArgs e)
        {
            try
            {
                sp = new SanPhamKho();
                sp.MaSo = -1;
                sp.Ten = txtTenSP.Text;
                sp.ChuDe = cbxChuDeSP.SelectedItem as ChuDe;
                sp.GiaBan = int.Parse(txtGiaBanSP.Text);
                sp.GiaMua = int.Parse(txtGiaMuaSP.Text);
                sp.BaoHanh = (int)txtBaoHanhSP.Value;
                sp.NgaySX = txtNgaySXSP.Value;
                sp.SoManhGhep = int.Parse(txtSoManhSP.Text);
                sp.HinhAnh = imgName;
                sp.IsVisible = chkbxDangKinhDoanh.Checked;
                bss.Add(sp);

                /*try
                {
                    bss.UploadImage(imgURL, imgName);
                }
                catch
                {
                    MessageBox.Show("Không thể upload lên server!");
                }*/

                MessageBox.Show("Đã thêm sản phẩm!");
            }
            catch
            {
                MessageBox.Show("Vui lòng nhập đầy đủ thông tin!");
            }
        }

        private void buttonLuu_Click(object sender, EventArgs e)
        {
            sp.Ten = txtTenSP.Text;
            sp.ChuDe = cbxChuDeSP.SelectedItem as ChuDe;
            sp.GiaBan = int.Parse(txtGiaBanSP.Text);
            sp.GiaMua = int.Parse(txtGiaMuaSP.Text);
            sp.BaoHanh = (int)txtBaoHanhSP.Value;
            sp.NgaySX = txtNgaySXSP.Value;
            sp.SoManhGhep = (int)txtSoManhSP.Value;
            sp.IsVisible = chkbxDangKinhDoanh.Checked;
            if(!string.IsNullOrEmpty(imgName)) sp.HinhAnh = imgName;
            bss.Edit(sp);
            //try
            //{
                //bss.UploadImage(imgURL, imgName);
            bss.UploadFile(imgURL);
            //}
            //catch
            //{
            //    MessageBox.Show("Không thể upload lên server!");
            //}
            this.Close();
        }

        private void buttonChooseImg_Click(object sender, EventArgs e)
        {
            string fileURL = "";
            OpenFileDialog dlg = new OpenFileDialog();
            dlg.Filter = "Image Files (*.*)|*.*|PNG Files (*.png)|*.png|JPG Files (*.jpg)|*.jpg|GIF Files (*.gif)|*.gif|All Files (*.*)|*.*";
            DialogResult result = dlg.ShowDialog();
            if (result == DialogResult.OK)
            {
                string newPathToFile = System.IO.Directory.GetCurrentDirectory().Replace(@"\bin\Debug", @"\img\" + dlg.SafeFileName);
                string newPathToFile2 = System.IO.Directory.GetCurrentDirectory().Replace(@"\bin\Debug", "\\..\\CuaHangLego.Webform\\img\\" + dlg.SafeFileName);
                //string newPathToFile = "../../img/" + dlg.SafeFileName;
                fileURL = newPathToFile;
                try
                {
                    imgName = dlg.SafeFileName;
                    imgURL = dlg.FileName;
                    File.Copy(dlg.FileName, newPathToFile, true);
                    File.Copy(dlg.FileName, newPathToFile2, true);
                }
                catch
                {
                    MessageBox.Show("Không thể sao chép hình ảnh!");
                }
                finally
                {
                    pictureSP.ImageLocation = newPathToFile;
                }
            }
        }
    }
}
